package com.gugu.procedure.controller;


import com.gugu.procedure.common.Constant;
import com.gugu.procedure.common.HttpServletRequestUtils;
import com.gugu.procedure.common.MessageResult;
import com.gugu.procedure.param.AccountUpdateParam;
import com.gugu.procedure.param.LoginParam;
import com.gugu.procedure.param.PasswordUpdateParam;
import com.gugu.procedure.service.AccountService;
import com.gugu.procedure.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = {"账号模块"})
@RequestMapping("account")
public class AccountController extends BaseController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private HttpServletRequestUtils httpServletRequestUtils;

    @PostMapping("open/login")
    @ApiOperation(value = "登录")
    public MessageResult login(@RequestBody LoginParam loginParam) {
        return accountService.login(loginParam.getAccount(), loginParam.getPassword());
    }

    @PostMapping("add")
    @ApiOperation(value = "新增")
    public MessageResult add(@RequestBody AccountUpdateParam accountUpdateParam) {
        return accountService.add(accountUpdateParam);
    }


    @GetMapping("updatePassword")
    @ApiOperation(value = "修改密码")
    public MessageResult updatePassword(String password) {
        return accountService.updatePassword(getAccount(), password);
    }

    @PostMapping("update")
    @ApiOperation(value = "管理员账号更新")
    public MessageResult update(HttpServletRequest request, @RequestBody AccountUpdateParam accountUpdateParam) {
        // 只能是本人或者超级管理员
        String role = httpServletRequestUtils.getRole(request);
        if (StringUtil.notEquals(role, Constant.SUPER_MANAGER_ROLE)) {
            MessageResult.error("权限不足");
        }
        return accountService.update(accountUpdateParam);
    }

    @PostMapping("updatePassword")
    @ApiOperation(value = "更新密码")
    public MessageResult updatePassword(HttpServletRequest request, @RequestBody PasswordUpdateParam passwordUpdateParam) {
        // 只能是本人或者超级管理员
        String role = httpServletRequestUtils.getRole(request);
        String account = httpServletRequestUtils.getAccount(request);
        if (StringUtil.isNotEmpty(passwordUpdateParam.getAccount())) {
            if (StringUtil.notEquals(role, Constant.SUPER_MANAGER_ROLE)) {
                MessageResult.error("权限不足");
            }
        } else {
            passwordUpdateParam.setAccount(account);
        }
        return accountService.updatePassword(passwordUpdateParam);
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public MessageResult page(String account, String role, String searchValue, int pageNo, int pageSize) {
        return accountService.page(account, role, searchValue, pageNo, pageSize);
    }


    @GetMapping("delete")
    @ApiOperation(value = "删除账号")
    public MessageResult delete(HttpServletRequest request, String account) {
        // 只能是超级管理员
        String role = httpServletRequestUtils.getRole(request);
        if (StringUtil.notEquals(role, Constant.SUPER_MANAGER_ROLE)) {
            MessageResult.error("权限不足");
        }
        return accountService.deleteAccount(account);
    }
}
